package com.hotel.dao.impl;

import com.hotel.dao.NotifyDao;
import com.hotel.entity.Notify;
import com.hotel.exception.EntityException;
import com.hotel.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author Str
 * @Date 2021/11/1 10:34
 * @Description :
 */
public class NotifyDaoImpl implements NotifyDao {
    @Override
    public void addNotify(Notify notify)
            throws SQLException {
        //{1}编写一条 sql 语句。
        String fields = "id,notifyName,uploader," +
                "fileName,fileSize, note,createDate";
        String sql = "insert into notify(" + fields + ")" +
                " values(?,?,?,?,?,?,now())";
        int count = JdbcUtils.update(sql, notify.getId(),
                notify.getNotifyName(),
                notify.getUploader(),
                notify.getFileName(),
                notify.getFileSize(),
                notify.getNote());
        if (count != 1) {
            throw new SQLException("新建通告失败。");
        }
    }

    @Override
    public void growDownloadCnt(String id) throws SQLException {
        // 增长下载数
        String sql = "update notify set downTimes=downTimes+1 where id=?";
        int cnt = JdbcUtils.update(sql, id);
        if (cnt != 1) {
            throw new SQLException("更新下载数失败");
        }
    }

    @Override
    public Notify getNotifyById(String id) throws SQLException, EntityException {
        String sql = "select n.*, u.nickName as uploaderName from notify n " +
                "left join user u on u.id=n.uploader where n.id = ? ";
        Notify notify = JdbcUtils.queryObject(sql, Notify.class, id);
        if (notify != null) {
            return notify;
        }
        throw new EntityException("获取不到通知信息.");
    }

    @Override
    public int update(Notify notify) throws SQLException {
        String sqlUpdate = "update notify set notifyName=?,uploader=?,fileName=?,note=? where id=?;";
        return JdbcUtils.update(sqlUpdate, notify.getNotifyName(), notify.getUploader(), notify.getFileName(), notify.getNote(), notify.getId());
    }

    /**
     * 获取搜索语句片段
     * map是url参数列表的map集合
     *
     * @return java.lang.String
     */
    @Override
    public String getSearchSQL(Map<String, Object> map) {
        //{ps}这个 map 进来这个方法之前先把 page, limit 拿掉。
        Set<String> keySet = map.keySet();
        StringBuilder sb = new StringBuilder();
        for (String key : keySet) {
            Object word = map.get(key);
            sb.append(" and " + key
                    + " like '%" + word + "%'");
        }
        return sb.toString();
    }

    /**
     * sqlPart是getSearchSQL方法的返回值
     *
     * @return int
     */
    @Override
    public int getCount(String sqlPart) throws SQLException {
        //{1}做 sql 拼接。
        String sql = "select count(*) from notify n " +
                " left join user u on u.id=n.uploader " +
                " where u.roleId!='*' " + sqlPart;
        System.out.println("getCount" + sql);
        //{2}查询数量。
        Long count = (Long) JdbcUtils.queryValue(sql);
        //{3}做类型转换 ----> int
        return count.intValue();
    }

    @Override
    public List<Notify> getList(String sqlPart, Integer offset, Integer limit) throws SQLException {
        String sql = "select n.*, u.nickName from notify n " +
                " left join user u on u.id=n.uploader" +
                " where u.roleId!='*' " + sqlPart + " order by n.id limit ?, ?";
        List<Notify> list = JdbcUtils.queryList(sql, Notify.class, offset, limit);
        System.out.println("getNotifyList" + list);
        return list;
    }

    @Override
    public int delete(String id) throws SQLException {
        String sqlDelete = "delete from notify where id=?";
        return JdbcUtils.update(sqlDelete, id);
    }
}
